package cn.ffcs.uec.view.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Tab;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.staff.constant.UecStaffConstants;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uec.view.action.bean.UecChartStaffMainBean;
import cn.ffcs.uec.view.manager.UecStaffAllManager;
import cn.ffcs.uec.view.model.UecStaffAll;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.util.UomZkUtil;
/**
 * @desc 专家推荐首页
 * @author BMP
 * @time 2019-09-06
 */
@Controller
@Scope("prototype")
public class UecChartStaffMainComposer extends BasePortletComposer implements IPortletInfoProvider {

    private static final long serialVersionUID = -5929030282722775845L;

    private UecChartStaffMainBean bean = new UecChartStaffMainBean();

    private UecStaffAll uecStaffAll = new UecStaffAll();
	//方便人员转换
	private StaffAndOutStaff staffAndOutStaff = new StaffAndOutStaff();
	/*private UecImportantWorkManager uecImportantWorkManager = (UecImportantWorkManager) ApplicationContextUtil
			.getBean("uecImportantWorkManager");
	private UecArticleManager uecArticleManager = (UecArticleManager) ApplicationContextUtil
			.getBean("uecArticleManager");
	private UecInnovationManager uecInnovationManager = (UecInnovationManager) ApplicationContextUtil
			.getBean("uecInnovationManager");*/
	private UecStaffAllManager uecStaffAllManager = (UecStaffAllManager) ApplicationContextUtil
			.getBean("uecStaffAllManager");

    /**
     * 关闭窗口标志
     */
    private Boolean onCloseFlag;

    @Override
    public String getPortletId() {
        return super.getPortletId();
    }

    @Override
    public ThemeDisplay getThemeDisplay() {
        return super.getThemeDisplay();
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
        Components.wireVariables(comp, bean);
        bean.getLogDownNewListboxExt().setPortletInfoProvider(this);
       
    }

    /**
     * 界面初始化
     * 
     * @throws Exception
     */
    public void onCreate$uecChartStaffMainWin() throws Exception {
    	binbBean();
    }
    /**
	 * 获取全局变量，绑定数据导页面
	 * @Title: binbBean   
	 * @param: @throws InterruptedException      
	 * @return: void      
	 * @throws
	 */
	private void binbBean() throws InterruptedException{
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户不存在!", "错误提示");
			return;
		} else {
			String screenName = PlatformUtil.getCurrentUser().getScreenName();
			if("n".equals(screenName.substring(0,1).toLowerCase())) {
				screenName=screenName.substring(1);
			}
			uecStaffAll.setCode(screenName.toUpperCase());
			uecStaffAll = uecStaffAllManager.findUecStaffAll(uecStaffAll);
		}
		if (null == uecStaffAll) {
			Messagebox.show("人员信息获取失败！");
			return;
		}else {
			staffAndOutStaff.setPkPsndoc(uecStaffAll.getPkpsndoc());
			callTab();
		}
	}


    /**
     * 监听关闭窗口事件
     * 
     * @param event
     */
    public void onCloseOutStaffMainWindownResponse(final ForwardEvent event) {
        onCloseFlag = (Boolean) event.getOrigin().getData();
        if (onCloseFlag) {
            bean.getUecChartStaffMainWin().onClose();
        }
    }
    /**
     * 切换tab
     * 
     * @param event
     */
    public void onClickTab(ForwardEvent event) {
        try {
            Event origin = event.getOrigin();
            if (origin != null) {
                Component comp = origin.getTarget();
                if (comp != null && comp instanceof Tab) {
                    bean.setTab((Tab) comp);
                    callTab();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * tab响应
     * 
     * @throws Exception
     * 
     */
    public void callTab() {
        try {
            if (bean.getTab() == null) {
                bean.setTab(bean.getTabBox().getSelectedTab());
            }
            if (uecStaffAll != null) {
                String tab = this.bean.getTab().getId();
                if ("uecImportant".equals(tab)) {
                    Events.postEvent(UecStaffConstants.ON_UEC_IMPORTANT_QUERY,
                            this.bean.getUecImportantListboxExt(), uecStaffAll);
                } 
                else if ("uecInnovation".equals(tab)) {
                    Events.postEvent(UecStaffConstants.ON_UEC_INNOVATION_QUERY,
                            this.bean.getUecInnovationListboxExt(), uecStaffAll);
                } else if ("uecArticle".equals(tab)) {
                    Events.postEvent(UecStaffConstants.ON_UEC_ARTICLE_QUERY,
                            this.bean.getUecArticleListboxExt(), uecStaffAll);
                } 
            } else {
                // 清空数据
                bean.getUecArticleListboxExt().onCleanUecArticleResponse(null);
                bean.getUecImportantListboxExt().onCleanUecImportantResponse(null);
                bean.getUecInnovationListboxExt().onCleanUecInnovationResponse(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理点击后Tabs .
     * 
     * @param event
     */
   public void onCleanTabUecStaffInfoQueryResponse(final ForwardEvent event) {
            Events.postEvent(UecStaffConstants.ON_UEC_ARTICLE_QUERY, bean.getUecArticleListboxExt(),
                    null);
            Events.postEvent(UecStaffConstants.ON_UEC_IMPORTANT_QUERY, bean.getUecImportantListboxExt(),
                    null);
            Events.postEvent(UecStaffConstants.ON_UEC_INNOVATION_QUERY, bean.getUecInnovationListboxExt(),
                    null);
            callTab();
    }
}
